package ${packageName};

import java.util.Date;
import java.util.List;

import com.adinnet.aiyike.response.code.CCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.support.log.LogModule;
import org.support.log.support.ProcessEvent;
import org.support.mybatis.page.Page;
import org.support.mybatis.page.PageHelper;
import com.adinnet.aiyike.log.BuzEvent;
import com.adinnet.aiyike.log.BuzLog;
import com.adinnet.aiyike.dao.ItemRequireLogDao;
import com.adinnet.aiyike.response.code.BuzCode;
import org.support.ex.BuzEx;
import com.adinnet.aiyike.status.DeleteStatus;
import com.alibaba.fastjson.JSON;

import ${MODEL_URL};
import ${DAO_URL};
import ${SERVICE_URL};


/**
 * @author ${annotation.authorName}(${annotation.authorMail})<br>
 * @date ${annotation.date}
 * 
 * @version ${annotation.version}
 */
@Service
public class ${className}${classSuffix} implements ${servicePrefix}${className}${serviceSuffix}{

    private static LogModule log = BuzLog.log();

    @Autowired
    private ${daoPrefix}${className}${daoSuffix}  ${classNameLower}${daoSuffix};
	
	
	<#list colList as col>
    <#if col.isPK==1>
    @Override
    public ${col.simpleFieldType} save(${className} ${classNameLower}){
        //log.info(BuzEvent., ProcessEvent.START, "${className}:" + JSON.toJSONString(${classNameLower}));
		int result = ${classNameLower}${daoSuffix}.save(${classNameLower});
		if(result==0){
			//log.error(null, ProcessEvent.EX, "save ${classNameLower} failed");
            throw new BuzEx(null);
		}
		${col.simpleFieldType} ${col.columnName}=${classNameLower}.get${col.columnFirstUppder}();	
        //log.info(BuzEvent., ProcessEvent.END);
		return ${col.columnName};
	}
    </#if>
    </#list>	
	
	
	<#list colList as col>
    <#if col.isPK==1>
     @Override
     public int deleteByPrimaryKey(${col.simpleFieldType} ${col.columnName}){
    	int result =${classNameLower}${daoSuffix}.deleteByPrimaryKey(${col.columnName});
    	if(result==0){
			//log.error(null, BuzEvent.EX, "delete ${classNameLower} failed");
            throw new BuzEx(null);
		}
		return result;
     }
    </#if>
    </#list>
    
    
    <#list colList as col>
    <#if col.isPK==1>
    @Override
    public int updateByPrimaryKey(${className} ${classNameLower}){
        //log.info(BuzEvent, ProcessEvent.START, "${classNameLower}:" + JSON.toJSONString(${classNameLower}));
  	    int result =${classNameLower}${daoSuffix}.updateByPrimaryKey(${classNameLower});
    	if(result==0){
			//log.error(null, ProcessEvent.PROCESS, "${classNameLower} update failed");
            throw BuzEx.build(null);
		}
        //log.info(null, ProcessEvent.END);
		return result;
    }
    </#if>
    </#list>
    
    <#list colList as col>
    <#if col.isPK==1>
    @Override
    public ${className} selectByPrimaryKey(${col.simpleFieldType} ${col.columnName}){
    	${className} ${classNameLower}=${classNameLower}${daoSuffix}.selectByPrimaryKey(${col.columnName});
    	return ${classNameLower};
    }
    </#if>
    </#list>
    
    
    @Override
    public List<${className}> query${className}All(){
    	return ${classNameLower}${daoSuffix}.query${className}All();
    }
 
    @Override
    public Page<${className}> query${className}ByPage(PageHelper helper, ${className} ${classNameLower}) {
        int count = ${classNameLower}${daoSuffix}.countByPage(${classNameLower}, helper.getPage());
        Page<${className}> page = new Page<>(helper, count);
        if (count == 0) {
            page.setContent(null);
        } else {
            List<${className}> list = ${classNameLower}${daoSuffix}.queryByPage(${classNameLower}, helper.getPage());
            page.setContent(list);
        }
        return page;
    }
  
	<#list idxList as idx>
	@Override
    public List<${className}> query${className}By${idx.idxName}(<#list idx.colList as idxCol><#if idxCol_has_next>${idxCol.simpleFieldType} ${idxCol.columnName},<#else>${idxCol.simpleFieldType} ${idxCol.columnName}</#if></#list>){
		return ${classNameLower}${daoSuffix}.query${className}By${idx.idxName}(<#list idx.colList as idxCol><#if idxCol_has_next>${idxCol.columnName},<#else>${idxCol.columnName}</#if></#list>);
    }
	</#list>
}